<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserGroupController
 *
 * @author zangguoping
 */
class UserGroupController extends Controller {

    //put your code here
    public function actionIndex() {
        $usergroup = new UserGroup();
        $this->render('index', array('model' => $usergroup));
    }

    public function actionCreate() {
        $request = Yii::app()->request;
        $usergroup = new UserGroup();
        $this->performAjaxValidation($usergroup);
        if ($request->getIsPostRequest()) {
            $usergroup->attributes = $request->getPost('UserGroup');
            if ($usergroup->save()) {
                $this->render('/layouts/message', array('message' => '添加成功'));
                Yii::app()->end();
            }
        }
        $this->render('create', array('model' => $usergroup));
    }

    public function actionDelete() {
        $request = Yii::app()->request;
        $id = (int) $request->getParam('id');
        if ($id) {
            $usergroup = new UserGroup();
            $usergroup->deleteByPk($id);
        }
    }

    public function actionUpdate() {
        $request = Yii::app()->request;
        $usergroup = new UserGroup();
        $id = (int) $request->getParam('id');
        $model = $usergroup->findByPk($id);
        $this->performAjaxValidation($usergroup);
        if ($request->getIsPostRequest()) {
            $model->attributes = $request->getPost('UserGroup');
            if ($model->save()) {
                $this->render('/layouts/message', array('message' => Yii::t('default','修改成功')));
                Yii::app()->end();
            }
        }
        $this->render('update', array('model' => $model));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'usergroup') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}

?>
